package com.eqdd.yiqidian.widget;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.eqdd.yiqidian.DialogFragmentInputCustom;
import com.eqdd.yiqidian.R;
import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.components.support.RxAppCompatDialogFragment;

import java.util.concurrent.TimeUnit;

/**
 * Created by lvzhihao on 17-4-20.
 */

public class InputDialogFragment extends RxAppCompatDialogFragment {

    OnClickListener onClickListener;
    private DialogFragmentInputCustom inflate;
    private Dialog dialog;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (dialog == null) {
            dialog = new Dialog(getActivity(), R.style.BottomDialog);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
            LayoutInflater inflater = getActivity().getLayoutInflater();
            inflate = DataBindingUtil.inflate(inflater, R.layout.dialog_fragment_input, null, false);
            dialog.setContentView(inflate.getRoot());
            dialog.setCanceledOnTouchOutside(true); // 外部点击取消
            // 设置宽度为屏宽, 靠近屏幕底部。
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM; // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
            window.setAttributes(lp);
            inflate.etMsg.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    inflate.btSend.setEnabled(!TextUtils.isEmpty(s.toString()));
                }
            });
            inflate.etMsg.setFocusable(true);
            inflate.etMsg.setFocusableInTouchMode(true);
            inflate.etMsg.requestFocus();
//            getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            RxView.clicks(inflate.btSend)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe((v) -> {
                        if (onClickListener != null) {
                            onClickListener.onClick(inflate.etMsg.getText().toString());
                        }
                    });
        }
        inflate.etMsg.setText("");
        String hint = getArguments().getString("hint");
        inflate.etMsg.setHint(hint);
        return dialog;
    }
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        window.setAttributes(windowParams);
    }

    @Override
    public void onResume() {
        super.onResume();
        (new Handler()).postDelayed(new Runnable() {
            public void run() {
                InputMethodManager inManager = (InputMethodManager) inflate.etMsg.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 500);
    }

    public interface OnClickListener {
        /**
         * Called when a view has been clicked.
         */
        void onClick(String content);
    }
}
